/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.co.withone.osgi.gadget.flickrconnector;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;

/**
 * The FlickrConnectorSettingView class provide FlickrConnector setting view.
 * 
 * @author funakky
 */
public class FlickrConnectorSettingView extends JFrame {

	private static final String KEY_TITLE = "setting.view.title";
	private static final String KEY_YAHOOID = "yahooid";
	private static final String KEY_APIKEY = "apikey";

	static String yahooID = null;
	static String apikey = null;

	/**
	 * Create new FlickrConnectorSettingView object so that initilized
	 * components.
	 */
	public FlickrConnectorSettingView() {
		super(Activator.RB.getString("setting.view.title"));
		initComponents();
	}

	private void initComponents() {
		final JFrame frame = this;

		// create components
		// final JLabel dirLabel = new
		// JLabel(PicasaConnectorActivator.RB.getString("dir.path"));

		final JLabel nameLabel = new JLabel(Activator.RB.getString(KEY_YAHOOID));
		final JTextField nameText = new JTextField();
		nameText.setPreferredSize(new Dimension(200, 20));

		final JLabel passLabel = new JLabel(Activator.RB.getString(KEY_APIKEY));
		final JPasswordField passText = new JPasswordField();
		passText.setPreferredSize(new Dimension(200, 20));

		final JButton okButton = new JButton(Activator.RB.getString("ok"));
		final JButton applyButton = new JButton(Activator.RB.getString("apply"));
		final JButton cancelButton = new JButton(Activator.RB
				.getString("cancel"));

		final ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				final Object source = e.getSource();
				if (source == okButton || source == applyButton) {
					yahooID = nameText.getText();
					char[] chars = passText.getPassword();
					apikey = String.valueOf(chars);
				}
				if (source == okButton || source == cancelButton) {
					frame.dispose();
				}
			}
		};

		okButton.addActionListener(actionListener);
		applyButton.addActionListener(actionListener);
		cancelButton.addActionListener(actionListener);

		// set layout
		final GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);

		{
			// set horizontal group
			final ParallelGroup pg1 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg1.addComponent(nameLabel).addComponent(passLabel).addComponent(
					okButton);
			final ParallelGroup pg2 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg2.addComponent(nameText).addComponent(passText).addComponent(
					applyButton);
			final ParallelGroup pg3 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg3.addComponent(cancelButton);

			layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
					pg1).addGroup(pg2).addGroup(pg3));
		}
		{
			// set vertical group
			final ParallelGroup pg1 = layout
					.createParallelGroup(Alignment.BASELINE);
			pg1.addComponent(nameLabel).addComponent(nameText);

			final ParallelGroup pg2 = layout
					.createParallelGroup(Alignment.BASELINE);
			pg2.addComponent(passLabel).addComponent(passText);

			final ParallelGroup pg3 = layout
					.createParallelGroup(Alignment.BASELINE);
			pg3.addComponent(okButton).addComponent(applyButton).addComponent(
					cancelButton);

			layout.setVerticalGroup(layout.createSequentialGroup()
					.addGroup(pg1).addGroup(pg2).addGroup(pg3));
		}

		pack();
	}
}
